package com.ym.jiujiangeyou.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 * @Description: 文件管理类
 */
public class FileManager {

	private static final int BUFFER = 8192;
	
	
	/** 
	* @return  
	* @retur  boolean 
	* @Description:  判断SD卡
	*/
	public static boolean hasSDCard() {
		String status = Environment.getExternalStorageState();
		if (!status.equals(Environment.MEDIA_MOUNTED)) {
			return false;
		}
		return true;
	}

	/** 
	* @return  
	* @retur  String 
	* @Description: 根目录 路径 
	*/
	public static String getRootFilePath() {
		if (hasSDCard()) {
			return Environment.getExternalStorageDirectory().getAbsolutePath()
					+ "/";// filePath:/sdcard/
		} else {
			return Environment.getDataDirectory().getAbsolutePath() + "/data/"; // filePath:
																				// /data/data/
		}
	}

	
	/** 
	* @return  
	* @retur  String 
	* @Description: 图片目录
	*/
	public static String getSaveFilePath() {
		if (hasSDCard()) {
			return getRootFilePath() + "jiujiangeyou/images/";
		} else {
			return getRootFilePath() + "jiujiangeyou/images/";
		}
	}
	
	/** 
	* @return  
	* @retur  String 
	* @Description: 下载目录 
	*/
	public static String getSaveApkPath() {
		if (hasSDCard()) {
			return getRootFilePath() + "jiujiangeyou/apk";
		} else {
			return getRootFilePath() + "jiujiangeyou/apk";
		}
	}

	
	/** 
	* @param imageUrl
	* @return  
	* @retur  Bitmap 
	* @Description: 获取图片对象 
	*/
	public static Bitmap getBitmap(String imageUrl) {
		if (isFileExists(imageUrl)) {
			// System.out.println("------"+getImagePath(imageUrl));
			return BitmapFactory.decodeFile(getImagePath(imageUrl));
		}
		return null;

	}

	/** 
	* @param imageUrl
	* @return  
	* @retur  String 
	* @Description: 获取图片路径 
	*/
	public static String getImagePath(String imageUrl) {
		int lastSlashIndex = imageUrl.lastIndexOf("/");
		String imageName = imageUrl.substring(lastSlashIndex + 1);
		String imageDir = getSaveFilePath();
		File file = new File(imageDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		String imagePath = imageDir + imageName;
		return imagePath;
	}

	/** 
	* @param imageUrl
	* @return  
	* @retur  boolean 
	* @Description: 判断图片是否存在
	*/
	public static boolean isFileExists(String imageUrl) {
		return new File(getImagePath(imageUrl)).exists();
	}
	
	/** 
	* @param imageUrl
	* @return  
	* @retur  File 
	* @Description: 获取图片文件 
	*/
	public static File getFile(String imageUrl){
		File f = new File(getImagePath(imageUrl)) ;
		return f ;
	}
	
	/** 
	* @param path
	* @return  
	* @retur  boolean 
	* @Description: 检查路径是否存在 
	*/
	public static boolean isFilePathExists(String path) {
		return new File(path).exists();
	}

	// 读取文件
	public static String readTextFile(File file) throws IOException {
		String text = null;
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			text = readTextInputStream(is);
			;
		} finally {
			if (is != null) {
				is.close();
			}
		}
		return text;
	}
	// 从流中读取文件
		public static String readTextInputStream(InputStream is) throws IOException {
			StringBuffer strbuffer = new StringBuffer();
			String line;
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new InputStreamReader(is));
				while ((line = reader.readLine()) != null) {
					strbuffer.append(line).append("\r\n");
				}
			} finally {
				if (reader != null) {
					reader.close();
				}
			}
			return strbuffer.toString();
		}
		// 将文本内容写入文件
		public static void writeTextFile(File file, String str) throws IOException {
			DataOutputStream out = null;
			try {
				out = new DataOutputStream(new FileOutputStream(file));
				out.write(str.getBytes());
				out.flush() ;
			} finally {
				if (out != null) {
					out.close();
				}
			}
		}

		// 复制文件
		public static void copyFile(File sourceFile, File targetFile)
				throws IOException {
			BufferedInputStream inBuff = null;
			BufferedOutputStream outBuff = null;
			try {
				inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
				outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
				byte[] buffer = new byte[BUFFER];
				int length;
				while ((length = inBuff.read(buffer)) != -1) {
					outBuff.write(buffer, 0, length);
				}
				outBuff.flush();
			} finally {
				if (inBuff != null) {
					inBuff.close();
				}
				if (outBuff != null) {
					outBuff.close();
				}
			}
		}
		
		/** 
		* @param dir
		* @return  
		* @retur  long 
		* @Description: 获取目录文件大小
		*/
		public static long getDirSize(File dir) {
			if (dir == null) {
				return 0;
			}
			if (!dir.isDirectory()) {
				return 0;
			}
			long dirSize = 0;
			File[] files = dir.listFiles();
			for (File file : files) {
				if (file.isFile()) {
					dirSize += file.length();
				} else if (file.isDirectory()) {
					dirSize += file.length();
					dirSize += getDirSize(file); // 递归调用继续统计
				}
			}
			return dirSize;
		}
	
	/** 
	*   
	* @retur  void 
	* @Description:  清除图片缓存
	*/
	public static void clearImageCache() {
		File file = new File(getSaveFilePath());
		if(!file.exists()) return ;
		File[] files = file.listFiles();
		if (files.length > 0) {
			for (File f : files) {
				f.delete();
			}
			 file.delete() ;
		}
	}
	
	/** 
	*   
	* @retur  void 
	* @Description: 删除安装文件
	*/
	public static void clearApkFile(){
		File file = new File(getSaveApkPath()+"/");
		if(!file.exists()) return ;
		File[] files = file.listFiles();
		if (files.length > 0) {
			for (File f : files) {
				f.delete();
			}
			 file.delete() ;
		}
	}
	
	/** 
	*   
	* @retur  void 
	* @Description: 删除文件夹 
	*/
	public static void clearDirs(){
		File file = new File(getRootFilePath()+"jiujiangeyou/") ;
		if(!file.exists()) return ;
		File[] files = file.listFiles();
		if (files.length > 0) {
			for (File f : files) {
				f.delete();
			}
			 file.delete() ;
		}
	}
	
	/** 
	* @param fileS
	* @return  
	* @retur  String B/KB/MB/GB
	* @Description: 转换文件大小 
	*/
	public static String formatFileSize(long fileS) {
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "KB";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "MB";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}
	
	
}
